﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;

namespace System.Data
{
    /// <summary>
    /// Provides DataViewCollection functionality
    /// </summary>
    public class DataViewCollection : Collection<DataView>
    {
        #region Events
        /// <summary>
        /// Requests Master DataRow for specified DataRelation
        /// </summary>
        internal event EventHandler<DataSetEventArgs> MasterDataRowCollectionRequest;

        /// <summary>
        /// INotifyPropertyChanged interface implementation
        /// </summary>
        internal event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        internal DataViewCollection()
        {
        }
        #endregion

        #region Indexers
        /// <summary>
        /// Gets DataView with specified name
        /// </summary>
        /// <param name="name">DataView name</param>
        /// <returns>DataView if found, otherwise null</returns>
        public DataView this[string name]
        {
            get
            {
                // Validate argument
                if (string.IsNullOrEmpty(name))
                {
                    throw new ArgumentNullException("name");
                }

                // Do case insensitive search
                string caseInsensitiveName = name.ToUpper(CultureInfo.InvariantCulture);
                foreach (DataView dataView in this)
                {
                    if (dataView.ViewName.ToUpper(CultureInfo.InvariantCulture).Equals(caseInsensitiveName))
                    {
                        return dataView;
                    }
                }

                return null;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Adds DataView to DataViewCollection
        /// </summary>
        /// <param name="dataView">DataView for adding</param>
        /// <returns>Added DataView</returns>
        public new DataView Add(DataView dataView)
        {
            // Validate argument
            if (dataView == null)
            {
                throw new ArgumentNullException("dataView");
            }

            if (string.IsNullOrEmpty(dataView.ViewName))
            {
                dataView.ViewName = "View" + (base.Count + 1);
            }

            dataView.MasterDataRowCollectionRequest += new EventHandler<DataSetEventArgs>(DataView_MasterDataRowRequest);
            dataView.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(DataView_PropertyChanged);

            base.Add(dataView);
            if (dataView.HasErrors)
            {
                this.DataView_PropertyChanged(dataView, new PropertyChangedEventArgs("HasErrors"));
            }

            return dataView;
        }

        /// <summary>
        /// Removes DataView from DataViewCollection
        /// </summary>
        /// <param name="dataView">DataView for removing</param>
        public new void Remove(DataView dataView)
        {
            // Validate argument
            if (dataView == null)
            {
                throw new ArgumentNullException("dataView");
            }

            dataView.MasterDataRowCollectionRequest -= DataView_MasterDataRowRequest;
            dataView.PropertyChanged -= DataView_PropertyChanged;
            base.Remove(dataView);
        }
        #endregion

        #region Event Handlers
        /// <summary>
        /// Handles DataView.MasterDataRowCollectionRequest event
        /// </summary>
        /// <param name="sender">Event source</param>
        /// <param name="e">Event arguments</param>
        void DataView_MasterDataRowRequest(object sender, DataSetEventArgs e)
        {
            // Raise MasterDataRowCollectionRequest event
            if (MasterDataRowCollectionRequest != null)
            {
                MasterDataRowCollectionRequest(sender, e);
            }
        }

        /// <summary>
        /// Handles DataTable.PropertyChanged event
        /// </summary>
        /// <param name="sender">Event source</param>
        /// <param name="e">Event arguments</param>
        void DataView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }
        }
        #endregion
    }
}
